﻿using System;
using System.Web;
using System.Text.RegularExpressions;

namespace X.Web
{
    public class HttpModule : IHttpModule
    {
        /// <summary>
        /// You will need to configure this module in the web.config file of your
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpModule Members

        public void Dispose()
        {
            //clean-up code here.
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(BeginRequest);
        }

        /// <summary></summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void BeginRequest(object sender, EventArgs e)
        {
            HttpContext context = ((HttpApplication)sender).Context;
            string requestPath = context.Request.Path;

            //TODO:这里后续可以采用配置文件的形式

            #region 产品静态页面地址重写到动态页面地址

            if (requestPath.StartsWith("/Product/", StringComparison.OrdinalIgnoreCase))
            {
                string[] arrPath = requestPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

                //Dec 09,2012
                //目前有个别产品名称包含/
                //类别是没有
                //这个不太好搞

                //产品列表
                //Product/类别.aspx
                if (2 == arrPath.Length)
                {
                    //Product/p1-vs-p2.aspx
                    if (arrPath[1].ToLower().Contains("_vs_"))
                    {
                        string[] arrPn = arrPath[1].Remove(arrPath[1].IndexOf(".")).Split(new string[] { "_vs_" }, StringSplitOptions.RemoveEmptyEntries);
                        context.RewritePath(string.Format("/Product/Compare.aspx?p1={0}&p2={1}", arrPn[0], arrPn[1]));
                        return;
                    }

                    //Product/类别.aspx
                    context.RewritePath(string.Format("/Product/List.aspx?CategoryName={0}", arrPath[1].Remove(arrPath[1].LastIndexOf("."))));
                    return;
                }

                //产品信息
                //Product/类别/产品名称.aspx
                if (3 == arrPath.Length)
                {
                    context.RewritePath(string.Format("/Product/Detail.aspx?CategoryName={0}&ProductName={1}", arrPath[1], arrPath[2].Remove(arrPath[2].IndexOf('.'))));
                    return;
                }
            }

            #endregion 产品静态页面地址重写到动态页面地址

            #region 价格

            if (requestPath.StartsWith("/Price", StringComparison.OrdinalIgnoreCase))
            {
                string tmp = requestPath.Substring(requestPath.IndexOf("-"));
                string[] arrPrice = tmp.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);

                context.RewritePath(string.Format("/Product/Price.aspx?LowPrice={0}&HightPrice={1}", arrPrice[0], arrPrice[1].Remove(arrPrice[1].IndexOf("."))));
                return;
            }

            #endregion 价格

            //新闻
            if (requestPath.StartsWith("/News/", StringComparison.OrdinalIgnoreCase))
            {
                if (requestPath.Equals("/News/List.aspx", StringComparison.OrdinalIgnoreCase))
                {
                    return;
                }

                string[] arrPath = requestPath.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

                context.RewritePath(string.Format("/News/Detail.aspx?Id={0}", arrPath[1].Remove(arrPath[1].IndexOf("."))));
                return;
            }
        }

        #endregion
    }
}
